import { getDeptStaffTreeData, listDept } from '@/api/system/dept'
import { treeselect } from '@/api/rsgl/zzjg'
import { getTaxauthorityList } from '@/api/common/common'

import { listCategory } from '@/api/productMange/product'
import { listWarehouse } from '@/api/warehouseManage/warehouse'
import useUserStore from '@/store/modules/user'
import { listJob } from '@/api/rsgl/ygda'
import { getConfigKey } from '@/api/system/config'
const userStore = useUserStore()
const useCommonStore = defineStore('common', {
  state: () => ({
    taxTreeData: [], //税务机关下拉树
    companyOptions: [], //归属公司
    useTreeData: [], // 项目负责人
    departmentData: [], // 归属部门
    productTreeData: [], //产品分类
    warehouseData: [], //仓库数据
    PersonTreeData: [],
    deptTreeData: [], //部门树结构数据，包含单位与单位下部门
    postData: [], //岗位数据
    filesObj: {} //模板通用下载办法
  }),
  actions: {
    /**
     * 获取税务机关
     * @returns {Promise<unknown>}
     */
    getTaxTreeData() {
      return new Promise(resolve => {
        // 如果税务机关下拉树数据为空,则通过接口
        if (!this.taxTreeData.length) {
          getTaxauthorityList().then(res => {
            this.taxTreeData = res.data
            resolve(this.taxTreeData)
          })
        } else {
          resolve(this.taxTreeData)
        }
      })
    },
    /**
     * 获取归属公司数据
     * @returns {Promise<unknown>}
     */
    getCompanyData(param = {}) {
      return new Promise(resolve => {
        let data = { deptType: 1 }
        console.info('param', param)
        // 如果归属公司数据为空,则通过接口获取
        if (!this.companyOptions.length) {
          treeselect({ ...data, ...param }).then(res => {
            this.companyOptions = res.data
            resolve(this.companyOptions)
          })
        } else {
          resolve(this.companyOptions)
        }
      })
    },
    //获取人员数据
    getUseTreeData(params) {
      let p = {}
      if (!params) {
        p = { deptId: userStore.user['orgId'] }
      } else {
        p = params
      }
      return new Promise(resolve => {
        getDeptStaffTreeData(p).then(res => {
          this.useTreeData = res.data
          resolve(this.useTreeData)
        })
      })
    },
    //获取产品分类数据
    getProductTreeData() {
      return new Promise(resolve => {
        if (!this.productTreeData.length) {
          listCategory().then(res => {
            const tree = {
              id: 0,
              name: '根目录',
              children: res.data
            }
            // @ts-ignore
            this.productTreeData.push(tree)
            resolve(this.productTreeData)
          })
        } else {
          resolve(this.productTreeData)
        }
      })
    },
    //获取仓库数据
    getWarehouseData() {
      return new Promise(resolve => {
        if (!this.warehouseData.length) {
          listWarehouse().then(res => {
            this.warehouseData = res.data
            resolve(this.warehouseData)
          })
        } else {
          resolve(this.warehouseData)
        }
      })
    },
    // 归属部门
    getDepartmentData() {
      return new Promise(resolve => {
        if (!this.departmentData.length) {
          treeselect().then(res => {
            this.departmentData = res.data
            resolve(this.departmentData)
          })
        } else {
          resolve(this.departmentData)
        }
      })
    },
    /**
     *     获取部门树结构数据
     *     包含 单位与下属部门
     */

    getDeptTreeData() {
      return new Promise(resolve => {
        if (!this.deptTreeData.length) {
          listDept().then(res => {
            this.deptTreeData = res.data
            resolve(this.deptTreeData)
          })
        } else {
          resolve(this.deptTreeData)
        }
      })
    },
    /**
     * 获取岗位数据
     */
    getPostData() {
      return new Promise(resolve => {
        if (!this.postData.length) {
          listJob().then(res => {
            //@ts-ignore
            this.postData = res.data
            resolve(this.postData)
          })
        } else {
          resolve(this.postData)
        }
      })
    },

    /**
     * 获取模板下载地址
     * @param paramName 模板参数名 系统设置-参数设置-参数键名
     */
    getUploadTemplateDown(paramName) {
      let baseUrl = `${import.meta.env.VITE_VIEW_FILEDOWN_URL}/file/`
      console.info('baseUrl', this.filesObj[paramName])
      return new Promise(resolve => {
        if (!this.filesObj[paramName]) {
          getConfigKey(paramName).then(res => {
            //@ts-ignore
            this.filesObj[paramName] = res.msg
            resolve(baseUrl + this.filesObj[paramName])
          })
        } else {
          resolve(baseUrl + this.filesObj[paramName])
        }
      })
    }
  }
})
export default useCommonStore
